<template>
    <div>
        <!-- 首行标签 -->
        <el-row >
            <el-col :span="6">
                <el-card shadow="hover" :body-style="{padding: '0px'}">
                    <div class="grid-content grid-con-4">
                            <i class="el-icon-sugar grid-con-icon" />
                        <div class="grid-cont-right">
                            <div class="grid-num">{{dashboardInfo.daily}}</div>
                            <div>今日新增</div>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6" class="top-tag">
                <el-card shadow="hover" :body-style="{padding: '0px'}">
                    <div class="grid-content grid-con-1">
                        <i class="el-icon-view grid-con-icon"></i>
                        <div class="grid-cont-right">
                            <div class="grid-num">{{dashboardInfo.weekly}}</div>
                            <div>7天新增</div>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6" class="top-tag">
                <el-card shadow="hover" :body-style="{padding: '0px'}">
                    <div class="grid-content grid-con-2">
                        <i class="el-icon-message grid-con-icon" ></i>
                        <div class="grid-cont-right">
                            <div class="grid-num">{{dashboardInfo.month}}</div>
                            <div>本月新增</div>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6" class="top-tag">
                <el-card shadow="hover" :body-style="{padding: '0px'}">
                    <div class="grid-content grid-con-3">
                        <i class="el-icon-goods grid-con-icon"></i>
                        <div class="grid-cont-right">
                            <div class="grid-num">3460</div>
                            <div>系统消息</div>
                        </div>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <!-- second echar -->
        <el-row :gutter="24">
            <el-col :span="10">
                <el-card shadow="hover">
                    <div slot="header" class="clearfix">
                        <span>流量来源分析</span>
                        <!--<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>-->
                    </div>
                    <div class="echart-box">
                        <div id="pieChart" style="height: 400px; "></div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="14" style="padding-left: 14px">
                <el-row>
                    <el-card shadow="hover">
                        <div slot="header" class="clearfix">
                            <span>本地区降水量分析</span>
                            <!--<el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>-->
                        </div>
                        <div class="echart-box">
                            <div id="mainChart" style="height: 400px; "></div>
                        </div>
                    </el-card>
                </el-row>
            </el-col>
        </el-row>
        <el-row :gutter="24">
            <el-col :span="10">
                <el-card shadow="hover" :body-style="{ height: '304px'}">
                    <div slot="header" class="clearfix">
                        <span>待办事项</span>
                        <el-button style="float: right; padding: 3px 0" type="text">添加</el-button>
                    </div>
                    <el-table :data="todoList" :show-header="false" height="304" style="width: 100%;font-size:14px;">
                        <el-table-column width="40">
                            <template slot-scope="scope">
                                <el-checkbox v-model="scope.row.status"></el-checkbox>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <div class="todo-item" :class="{'todo-item-del': scope.row.status}">
                                    {{scope.row.title}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column width="60">
                            <template slot-scope="scope">
                                <i class="el-icon-edit"></i>
                                <i class="el-icon-delete"></i>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>

            </el-col>
            <el-col :span="14">
                <div class="block">
                    <el-timeline>
                        <el-timeline-item timestamp="2018/4/12" placement="top">
                            <el-card>
                                <h4>更新 Github 模板</h4>
                                <p>王小虎 提交于 2018/4/12 20:46</p>
                            </el-card>
                        </el-timeline-item>
                        <el-timeline-item timestamp="2018/4/3" placement="top">
                            <el-card>
                                <h4>更新 Github 模板</h4>
                                <p>王小虎 提交于 2018/4/3 20:46</p>
                            </el-card>
                        </el-timeline-item>
                        <el-timeline-item timestamp="2018/4/2" placement="top">
                            <el-card>
                                <h4>更新 Github 模板</h4>
                                <p>王小虎 提交于 2018/4/2 20:46</p>
                            </el-card>
                        </el-timeline-item>
                    </el-timeline>
                </div>
            </el-col>

        </el-row>
    </div>
</template>

<script>
    import echarts from 'echarts'
    import {getDashboardInfo,getDashboardInfoList} from "../api/api";

    var colors = ['#5793f3', '#d14a61', '#675bba'];

    export default {
        name: 'dashboard',
        data() {
            return {
                name: localStorage.getItem('ms_username'),
                dashboardInfo:{
                    daily:56,
                    weekly:124,
                    month:241,
                },
                todoList: [
                    {
                        title: '今天要修复100个bug',
                        status: false,
                    },
                    {
                        title: '今天要修复100个bug',
                        status: false,
                    },
                    {
                        title: '今天要写100行代码加几个bug吧',
                        status: false,
                    }, {
                        title: '今天要修复100个bug',
                        status: false,
                    },
                    {
                        title: '今天要修复100个bug',
                        status: true,
                    },
                    {
                        title: '今天要写100行代码加几个bug吧',
                        status: true,
                    }
                ]
            }
        },
        methods: {
            drawPie(id) {
                this.charts = echarts.init(document.getElementById(id))
                this.charts.setOption({
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
                    },
                    series: [
                        {
                            name: '访问来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: [
                                {value: 335, name: '直接访问'},
                                {value: 310, name: '邮件营销'},
                                {value: 234, name: '联盟广告'},
                                {value: 135, name: '视频广告'},
                                {value: 1548, name: '搜索引擎'}
                            ],
                            itemStyle: {
                                emphasis: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                })
            },
            drawBar(id) {
                this.charts = echarts.init(document.getElementById(id))
                this.charts.setOption({
                    color: colors,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross',
                            crossStyle: {
                                color: '#999'
                            }
                        }
                    },
                    toolbox: {
                        feature: {
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    legend: {
                        data: ['蒸发量', '降水量', '平均温度']
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
                            axisPointer: {
                                type: 'shadow'
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            name: '水量',
                            min: 0,
                            max: 250,
                            interval: 50,
                            axisLabel: {
                                formatter: '{value} ml'
                            }
                        },
                        {
                            type: 'value',
                            name: '温度',
                            min: 0,
                            max: 25,
                            interval: 5,
                            axisLabel: {
                                formatter: '{value} °C'
                            }
                        }
                    ],
                    series: [
                        {
                            name: '蒸发量',
                            type: 'bar',
                            data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
                        },
                        {
                            name: '降水量',
                            type: 'bar',
                            data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
                        },
                        {
                            name: '平均温度',
                            type: 'line',
                            yAxisIndex: 1,
                            data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
                        }
                    ]
                })
            },
            getStatisticsInfo(){
                getDashboardInfo().then((res)=>{
                    if (res.status ) {
                        if (undefined !== res.data){
                            this.dashboardInfo.daily = res.data[0][0] ;
                            this.dashboardInfo.weekly = res.data[0][1];
                            this.dashboardInfo.month = res.data[0][2];
                        }
                    } else {
                        this.$message({
                            message: '加载失败',
                            type: 'error',
                        });
                    }
                })
            },
            getProxyStatisInfo(){
                getDashboardInfoList().then((res)=>{
                    if (res.status ) {
                        this.total = parseInt(res.data.totalNum);
                        this.users = res.data.itemList;
                        this.listLoading = false;
                        this.$message({
                            message: '加载成功',
                            type: 'success'
                        });
                    } else {
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                })
            }
        },
        computed: {
            role() {
                return this.name === 'admin' ? '超级管理员' : '普通用户';
            }
        },
        mounted() {
            this.$nextTick(function () {
                this.drawBar('mainChart')
                this.drawPie('pieChart')
            });
            this.getStatisticsInfo();
            this.getProxyStatisInfo();

        }
    }

</script>


<style scoped>
    .circle {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        background: #E83338;
    }

    .block {
        background-color: white;
        padding: 12px;
    }

    .top-tag {
        padding-left: 6px;
    }

    .el-row {
        margin-bottom: 20px;
    }

    .grid-content {
        display: flex;
        align-items: center;
        height: 100px;
    }

    .grid-cont-right {
        flex: 1;
        text-align: center;
        font-size: 12px;
        color: #999;
    }

    .grid-num {
        font-size: 30px;
        font-weight: bold;
    }

    .grid-con-icon {
        font-size: 50px;
        width: 100px;
        height: 100px;
        text-align: center;
        line-height: 100px;
        color: #fff;
    }

    .grid-con-2 .grid-con-icon {
        background: #1da1f2
    }

    .grid-con-2 .grid-num {
        color: #1da1f2
    }

    .grid-con-3 .grid-con-icon {
        background: rgb(100, 213, 114);
    }

    .grid-con-3 .grid-num {
        color: rgb(100, 213, 114);
    }

    .grid-con-1 .grid-con-icon {
        background: #e7b63a
    }

    .grid-con-1 .grid-num {
        color: #e7b63a
    }
    .grid-con-4 .grid-con-icon {
        background: #3b5998
    }

    .grid-con-4 .grid-num {
        color: #3b5998
    }

    .user-info {
        display: flex;
        align-items: center;
        padding-bottom: 20px;
        border-bottom: 2px solid #ccc;
        margin-bottom: 20px;
    }

    .user-avator {
        width: 120px;
        height: 120px;
        border-radius: 50%;
    }

    .user-info-cont {
        padding-left: 50px;
        flex: 1;
        font-size: 14px;
        color: #999;
    }

    .user-info-cont div:first-child {
        font-size: 30px;
        color: #222;
    }

    .user-info-list {
        font-size: 14px;
        color: #999;
        line-height: 25px;
    }

    .user-info-list span {
        margin-left: 70px;
    }

    .mgb20 {
        margin-bottom: 20px;
    }

    .todo-item {
        font-size: 14px;
    }

    .todo-item-del {
        text-decoration: line-through;
        color: #999;
    }

</style>
